package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;


    @Override//套餐列表
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize,String name) {
        Page<Setmeal> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Setmeal> w = new LambdaQueryWrapper<>();
        w.like(StrUtil.isNotEmpty(name),Setmeal::getName,name);
        page = setmealMapper.selectPage(page, w);
        List<Setmeal> list = page.getRecords();
        if (CollectionUtil.isNotEmpty(list)) {
            for (Setmeal setmeal : list) {
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());
                LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
                List<SetmealDish> list1 = setmealDishMapper.selectList(wrapper);
                setmeal.setSetmealDishes(list1);
            }
        }
        return page;
    }

    @Override//新增套餐
    @CacheEvict(value = "setmeal",key = "#setmeal.categoryId")
    public void save(Setmeal setmeal) {
        //1.新增套餐Setmeal
        setmealMapper.insert(setmeal);
        List<SetmealDish> list = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(list)) {
            for (SetmealDish setmealDish : list) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override//修改套餐回显
    public Setmeal findById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishMapper.selectList(wrapper);
        setmeal.setSetmealDishes(list);
        return setmeal;
    }

    @Override//修改套餐
    @CacheEvict(value = "setmeal",allEntries = true)
    public void update(Setmeal setmeal) {
        setmealMapper.updateById(setmeal);
        List<SetmealDish> list = setmeal.getSetmealDishes();
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            for (SetmealDish setmealDish : list) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }
    @Override//批量删除、删除
    @CacheEvict(value = "setmeal",allEntries = true)
    public void delete(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        Integer count = setmealMapper.selectCount(wrapper);
        if (count>0) {
            throw new CustomException("删除的套餐必须为停售状态!");
        }
        setmealMapper.deleteBatchIds(ids);
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(wrapper1);
    }

    @Override//批量启售、禁售
    @CacheEvict(value = "setmeal",allEntries = true)
    public void updateStatus(Integer status, List<Long> ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        Setmeal setmeal=new Setmeal();
        setmeal.setStatus(status);
        setmealMapper.update(setmeal,wrapper);
    }
    //h5套餐展示
    @Override
    @Cacheable(value = "setmeal",key = "#categoryId",unless = "#result==null||#result.size()==0")
    public List<Setmeal> findSeatmealList(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId).eq(Setmeal::getStatus,status);
        List<Setmeal> setmealList = setmealMapper.selectList(wrapper);
        return setmealList;
    }
    //显示套餐内容
    @Override
    public List<SetmealDish> findSetmealDishById(Long id) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            for (SetmealDish setmealDish : list) {
                Dish dish = dishMapper.selectById(setmealDish.getDishId());
                setmealDish.setImage(dish.getImage());
                setmealDish.setDescription(dish.getDescription());
            }
        }
        return list;
    }

}
